package com.leyou.sh.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.sh.auth.entity.ClientInfo;
import com.leyou.sh.auth.mapper.ClientInfoMapper;
import com.leyou.sh.auth.service.ClientAuthService;
import com.leyou.sh.common.exception.LyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author balisong_fcs
 * @date 2021/1/11 23:34
 * @desc
 */
@Service
public class ClientAuthServiceImpl extends ServiceImpl<ClientInfoMapper, ClientInfo> implements ClientAuthService {
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Value("${ly.jwt.key}")
    private String key;

    @Override
    public String getKey(String clientId, String secret) {

        ClientInfo clientInfo = this.query().eq("client_id", clientId).one();

        //如果传入的clientId有误,clientInfo为空,或者secret不匹配,否返回403
        if (null == clientInfo || !passwordEncoder.matches(secret,clientInfo.getSecret())) {
            throw new LyException(403, "非法客户端请求");
        }
        return key;
    }
}
